<!--
 * @Author: caofujun
 * @Date: 2020-10-23 16:00:59
 * @Description:
-->
<template>
  <div>
    <el-date-picker
      v-model="value"
      v-bind="$attrs"
      :type="type"
      :value-format="valueFormat"
      :default-time="defaultTime"
      :picker-options="newPickerOptions"
      :size="size"
      :range-separator="rangeSeparator"
      :start-placeholder="startPlaceholder"
      :end-placeholder="endPlaceholder"
      @change="changeVal"
    />
  </div>
</template>

<script>
// import { DatePickerOptions } from 'element-ui'
import { calendarShortcuts } from '@/utils/shortcuts'
import { disbledDateFun, DisabledDateTypeMap } from '@/utils'

export default {
  name: 'DateRangePicker',
  props: {
    type: {
      type: String,
      default: 'datetimerange'
    },
    valueFormat: {
      type: String,
      default: 'yyyy-MM-dd HH:mm:ss'
    },
    disabledDateType: {
      type: String,
      default: 'No'
    },
    size: {
      type: String,
      default: 'small'
    },
    rangeSeparator: {
      type: String,
      default: ':'
    },
    startPlaceholder: {
      type: String,
      default: '开始日期'
    },
    endPlaceholder: {
      type: String,
      default: '结束日期'
    },
    dateVal: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      defaultTime: ['00:00:00', '23:59:59'],
      value: []
    }
  },
  computed: {
    newPickerOptions() {
      const pickerOptions = {
        shortcuts: calendarShortcuts,
        disabledDate: (time) => {
          return disbledDateFun(time, this.disabledDateType)
        }
      }
      return pickerOptions
    }
  },
  watch: {
    disabledDateType: {
      immediate: true,
      handler(newVal) {
        const date = new Date()
        if (newVal !== DisabledDateTypeMap.No) {
          this.defaultTime = ['00:00:00', date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()]
        }
      }
    },
    dateVal: {
      immediate: true,
      handler(val) {
        this.value = val
      }
    }
  },
  mounted() {
  },
  methods: {
    changeVal(val) {
      this.$emit('update:creatTime', val)
    }
  }
}
</script>
